<template>
  <section class="page-content">
    <div class="toolbar">
      <el-form :inline="true" :model="params" ref="formData" class="search-form">
        <el-form-item label="支付状态">
          <el-select v-model="formData.pay_status" size="small" placeholder="支付状态">
            <el-option
              label="全部"
              value="">
            </el-option>
             <el-option
              label="未支付"
              value="0">
            </el-option>
            <el-option
              label="已支付"
              value="1">
            </el-option>
             <el-option
              label="已退款"
              value="3">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发货状态">
          <el-select v-model="formData.shipping_status" size="small" placeholder="发货状态">
            <el-option
              label="全部"
              value="">
            </el-option>
            <el-option
              label="未发货"
              value="0">
            </el-option>
            <el-option
              label="已发货"
              value="1">
            </el-option>
            <el-option
              label="部分发货"
              value="2">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="formData.order_status" size="small" placeholder="订单状态">
            <el-option
              label="全部"
              value="">
            </el-option>
            <el-option label="待确认" value="UNCONFIRMED"></el-option>
            <el-option label="已确认" value="CONFIRMED"></el-option>
            <el-option label="已取消" value="CANCELLED"></el-option>
            <el-option label="已完成" value="FINISHED"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="formData.order_sn" size="small" placeholder="订单编号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" icon="search" @click="onSubmit">查询</el-button>
        </el-form-item>
       <!--  <el-form-item v-if="multipleSelection.length > 0">
          <el-button type="danger" size="mini" icon="plus" @click="multipleDelete">批量删除</el-button>
        </el-form-item> -->
      </el-form>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData.data"
      @sort-change="sort"
      border>
      <el-table-column
        prop="order_sn"
        sortable
        label="订单号">
      </el-table-column>
      <el-table-column
        prop="phone"
        sortable
        label="收件人">
        <template slot-scope="scope">
          {{scope.row.consignee}}（{{scope.row.phone}}）
        </template>
      </el-table-column>
      <el-table-column
        prop="total_amount"
        sortable
        label="总金额">
      </el-table-column>
      <el-table-column
        prop="order_amount"
        sortable
        label="应付金额">
      </el-table-column>
      <el-table-column
        prop="order_status"
        sortable
        label="订单状态">
         <template slot-scope="scope">
          {{scope.row.order_status_desc}}
        </template>
      </el-table-column>
      <el-table-column
        prop="shipping_status"
        sortable
        label="发货状态">
        <template slot-scope="scope">
          {{scope.row.shipping_status_desc}}
        </template>
      </el-table-column>
      <el-table-column
        prop="pay_status"
        sortable
        label="支付状态">
        <template slot-scope="scope">
          {{scope.row.pay_status_desc}}
        </template>
      </el-table-column>
      <el-table-column
        prop="pay_name"
        sortable
        label="支付方式">
      </el-table-column>
      <el-table-column
        prop="created_at"
        sortable
        label="下单时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="show(scope.row)"
            type="success"
            size="mini">
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
     <div class="block toolbar">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="tableData.current_page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="parseInt(tableData.per_page)"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total">
      </el-pagination>
    </div>
  </section>
</template>
<script>
  import {generateUrl} from '@/utils/helpers'
  export default {
    data () {
      return {
        loading: false,
        categories: '',
        multipleSelection: [],
        tableData: {
          first_page_url: '',
          from: '',
          to: '',
          last_page: '',
          last_page_url: '',
          next_page_url: '',
          prev_page_url: '',
          path: '',
          per_page: 0,
          total: 0,
          data: []
        },
        formData: {
          pay_status: '',
          order_status: '',
          shipping_status: '',
          order_sn: ''
        },
        params: {
          order_by: 'id',
          order_type: 'desc',
          page_size: 20,
          pay_status: '',
          order_status: '',
          shipping_status: '',
          order_sn: '',
          page: 1
        }
      }
    },
    mounted () {
      this._getData('/orders')
      this._getCategories()
    },
    watch: {
      params: {
        deep: true,
        handler (newValue) {
          this._getData(generateUrl('/orders', newValue))
        }
      }
    },
    methods: {
      deleteRow (row) {
        this.$confirm('确定要删除该产品?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http.delete('/orders/' + row.id).then((response) => {
            this.loading = false
            this.$message.success('删除成功')
            this._getData()
          }).catch((error) => {
            this.loading = false
            console.log(error)
          })
        }).catch(() => {
        })
      },
      multipleDelete () {
        console.log(this.multipleSelection)
      },
      show (item) {
        this.$router.push('/orders/' + item.id)
      },
      onSubmit () {
        this.params.pay_status = this.formData.pay_status
        this.params.order_status = this.formData.order_status
        this.params.shipping_status = this.formData.shipping_status
        this.params.order_sn = this.formData.order_sn
      },
      handleSizeChange (val) {
        this.params.page_size = val
      },
      handleCurrentChange (val) {
        this.params.page = val
      },
      sort (data) {
        let orderType = ''
        if (data.order === 'ascending') {
          orderType = 'asc'
        }
        if (data.order === 'descending') {
          orderType = 'desc'
        }
        this.params.order_type = orderType
        this.params.order_by = data.prop
      },
      handleSelectionChange (val) {
        this.multipleSelection = val
      },
      _getData (url) {
        url = generateUrl(url, this.params)
        this.loading = true
        this.$http.get(url).then((response) => {
          this.loading = false
          this.tableData = response.data
        }).catch((error) => {
          this.loading = false
          console.log(error)
        })
      },
      _getCategories () {
        this.$http.get('/categories?belongs_to=goods').then((response) => {
          this.categories = response.data
        }).catch((error) => {
          console.log(error)
        })
      }
    }
  }
</script>
<style scoped lang="scss">
  .table-expand {
    font-size: 0;
  }
  .table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 40%;
  }
  .search-form {
    .el-input {
      width: 120px;
    }
    .el-select {
      width: 120px;
    }
  }
</style>
